﻿using DoNet.Common.Helpers;
using DoNet.Common.Result;
using DoNet.Wechat.MP.Menu;
using DoNet.Wechat.MP.Menu.Entitys;
using Microsoft.AspNetCore.Mvc;
using WechatMp.Web.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WechatMp.Web.Controllers.API.MP
{
    [Route("MP/[controller]")]
    [ApiController]
    public class MenuController : ApiMPController
    {
        private readonly MenuApi menuApi;

        public MenuController(ILogger<MenuController> _logger, IServiceProvider _serviceProvider) : base(_logger, _serviceProvider)
        {
            menuApi = new MenuApi(logger, serviceProvider);
        }

        [HttpGet]
        [Route("GetMenus")]
        public IActionResult GetMenus(string appid, string secret)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(appid, secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var resultData = menuApi.GetCurrentMenu(accessToken);
                    //var resultData = menuApi.GetMenuText(accessToken);
                    if (resultData != null)
                    {
                        var errcode = (int)(resultData.errcode ?? 0);
                        if (errcode == 0)
                        {
                            result.Success = true;
                            result.ResData = resultData;
                        }
                        result.ErrCode = errcode.ToString();
                        result.ErrMsg = (string)(resultData.errmsg ?? string.Empty);
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }

        [HttpPost]
        [Route("CreateMenus")]
        public IActionResult CreateMenus([FromBody] MenusRequestModel model)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(model.appid, model.secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var buttonGroup = JsonHelper.DeserializeObject<MenuFull_ButtonGroup>(model.menus);
                    if (buttonGroup != null)
                    {
                        var resultData = menuApi.CreateMenu(accessToken, buttonGroup);
                        if (resultData != null)
                        {
                            if (resultData.errcode == 0)
                            {
                                result.Success = true;
                                result.ResData = resultData;
                            }
                            result.ErrCode = resultData.errcode.ToString();
                            result.ErrMsg = resultData.errmsg;
                        }
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }

        [HttpGet]
        [Route("DeleteMenu")]
        public IActionResult DeleteMenu(string appid, string secret)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(appid, secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var resultData = menuApi.DeleteMenu(accessToken);
                    if (resultData != null)
                    {
                        if (resultData.errcode == 0)
                        {
                            result.Success = true;
                            result.ResData = resultData;
                        }
                        result.ErrCode = resultData.errcode.ToString();
                        result.ErrMsg = resultData.errmsg;
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }
    }
}
